import frappe
from erpnext.stock.doctype.bin.bin import Bin
from zelin_subcontract.doc_events import update_reserved_qty_for_sub_contract

class CustomBin(Bin):
    def update_reserved_qty_for_sub_contracting(self, subcontract_doctype="Subcontracting Order"):
        """
        原生功能按料号汇总，共用原料超发数量影响别的订单预留量，
        使用doc_events.update_reserved_qty_for_sub_contract,重新实现了相关逻辑
        """
        pass

@frappe.whitelist()
def custom_update_subcontracting_order_status(sco, status=None):
    """
    确保手工修改委外订单状态时也触发自定义更新委外预留数量逻辑
    """

    from erpnext.subcontracting.doctype.subcontracting_order.subcontracting_order import (
        update_subcontracting_order_status)
    
    update_subcontracting_order_status(sco, status)
    if isinstance(sco, str):
        sco = frappe.get_doc("Subcontracting Order", sco)
    update_reserved_qty_for_sub_contract(sco, None)

   
def override_subcontracting_receipt_dashboard(data):
    items = data.get("transactions")[0].get("items")
    items.append("Purchase Invoice")
    return data 